import axios from 'axios'
import { getToken } from '@/utils/storage'
import { MessagePlugin } from 'tdesign-vue-next'
import router from '@/router'

const request = axios.create({
    baseURL: 'https://api-hmzs.itheima.net/api/',
    timeout: 3000
})

// 添加请求拦截器
request.interceptors.request.use(config => {
    config.headers.Authorization = `Bearer ${ getToken() }`
    return config
}, error => {
    return Promise.reject(error)
})

// 添加响应拦截器
request.interceptors.response.use(response => {
    return response.data
}, error => {
    MessagePlugin.error(error.response.data.msg)
    return Promise.reject(error)
})

export default request
